package com.git_taoxina.homework.test5;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class EnterMeeting implements Runnable {
    private int peopleNumber = 100;//编号：总人数为100
    private int qiaCount;//前门入场的人数
    private int houCount;//前门入场的人数
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (peopleNumber > 0) {
            ruChang();
        }
    }

    public void ruChang() {
        lock.lock();
//        定义一个集合用于存放每一个人的编号
        ArrayList<Integer> lists = new ArrayList<>();
        if (!lists.contains(peopleNumber)) {
            lists.add(peopleNumber);
        }
        try {
            Thread.sleep(100);//线程等待时间 毫秒值
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //判断当前的人数是否已全部进场完毕 若为0则表示全部入场，返回统计的结果并结束方法
        if (peopleNumber == 0) {
            System.out.println("从前门入场的员工总共：" + qiaCount + "位员工");
            System.out.println("从后门入场的员工总共：" + houCount + "位员工");
            return;
        }
        String name = Thread.currentThread().getName();//获取当前线程的名字
        if ("前门".equals(name)) {
            System.out.println("编号为：" + peopleNumber + "的员工 从前门 入场！" + "拿到的双色球号码是：" + DoubleColorBallUtil.create());
            qiaCount++;
        } else if ("后门".equals(name)) {
            System.out.println("编号为：" + peopleNumber + "的员工 从后门 入场！" + "拿到的双色球号码是：" + DoubleColorBallUtil.create());
            houCount++;
        }
        lock.unlock();
        peopleNumber--;
    }
}
